<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * zy_auth_mc_log Database of the driver class.
 *
 * @Author:    <mirze@foxmail.com>Mirze
 * @Package:   Database Operate class of the system.
 * @Copyright: Copyright (c) 2005 - 2006 
 * @Version:   $Id: DB_zy_auth_mc_log.class.php 2011-07-07 11:56:30 $
 * @Show Note: 设备验收记录表
 */
/* @Package of the database class */
Class DB_zy_auth_mc_log extends PdoMysql
{
    /* Mysql Table Name */
    public $tableName = "zy_auth_mc_log";

    /**
     * Construction this Database Object.
     *
     * @Author: Mirze.
     * @Show: Get the PdoMysql Object and Set the default Charset.
     * @Return: ..
     */
    public function __construct()
    {
    		parent::__construct();
    }

    /**
     * Execute the insert method.
     *
     * @Author: Mirze.
     * @Show: Execute 'insert' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Return: False OR int.
     */
    public function insert_zy_auth_mc_log($values)
    {
        return $this->insert($this->tableName, $values);
    }

    /**
     * Execute the update method.
     *
     * @Author: Mirze.
     * @Show: Execute 'update' method by the PdoMysql Object.
     * @Param: $values Array -- Key is the Table Coulnum and Value is that values.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function update_zy_auth_mc_log($values, $where)
    {
        return $this->update($this->tableName, $values, $where);
    }
    
    /**
     * Execute the delete method.
     *
     * @Author: Mirze.
     * @Show: Execute 'delete' method by the PdoMysql Object.
     * @Param: @where  Array -- Key is the Table Coulnum and Value is the where values.
     * @Return: False OR int.
     */
    public function delete_zy_auth_mc_log($where)
    {
        return $this->delete($this->tableName, $where);
    }

    /**
     * Execute the select method.
     *
     * @Author: Mirze
     * @Show: Execute 'select' method by the PdoMysql Object, Return the item set.
     * @Param: $where String -- Search where.
     * @Param: $col String -- Search Order.
     * @Param: $numrows Int -- Search item num.
     * @Param: $offset Int -- Search item set offset.
     * @Param: $field String -- Search item name.
     * @Return: False OR Array.
     */
    public function getAll($where='1', $col='', $numrows=-1, $offset=-1, $field="*")
    {
        $sql = sprintf("SELECT %s FROM %s WHERE %s", $field, $this->tableName, $where);
        return $this->select($sql, $col, $numrows, $offset);
    }

    /**
     * Get one Item.
     *
     * @Author: Mirze
     * @Show: Search the database.Return one item.
     * @Param: $index Array -- Key is the index Key.Value is the index value.
     * @Return: Array.
     */
    public function getOne($auth_mc_log_id)
    {
        $sql = sprintf("SELECT * FROM %s WHERE auth_mc_log_id = '%s'", $this->tableName, $auth_mc_log_id);
        $result = $this->select($sql, 'auth_mc_log_id', 1, 0);
        return isset($result[0]) ? $result[0] : array();
    }

	/**
	 * (功能描述)获取验收设备
	 *
	 * @param string contract_no 合同号
	 * @return
	 */
	public function getEquipment($contract_no, $col='', $numrows=-1, $offset=-1, $field="*")
	{
		/*
		SELECT * FROM zy_sell_reply zsr 
		LEFT JOIN zy_mc_log zml ON zsr.sell_reply_id = zml.sell_reply_id
		LEFT JOIN zy_no_log znl ON znl.mc_log_id = zml.mc_log_id
		WHERE zsr.contract_no=1 AND zml.auth_state=0 
		AND znl.sell_no>0 AND znl.arrival_no>0 AND znl.arrival_no>znl.auth_no
		*/
		$sql = sprintf("SELECT %s FROM zy_sell_reply zsr", $field);
		//$sql = "SELECT * FROM zy_sell_reply zsr ";
		$sql .= " LEFT JOIN zy_mc_log zml ON zsr.sell_reply_id = zml.sell_reply_id";
		$sql .= " LEFT JOIN zy_no_log znl ON znl.mc_log_id = zml.mc_log_id";
		$sql .= " WHERE zsr.contract_no='{$contract_no}' AND zml.auth_state=0 ";
		$sql .= " AND znl.sell_no>0 AND znl.arrival_no>0 AND znl.arrival_no>znl.auth_no";
		//echo $sql;
		$res =  $this->select($sql, $col, $numrows, $offset);

		return (is_array($res)) ? $res : array();
	}

	/**
	 * (功能描述) 获取验证ID的记录
	 *
	 * @param int auth_result 验证结果： 0 不通过 1 通过 -1 未知(所有)
	 * @return
	 */
	public function getRecord($mc_auth_id, $auth_result=-1, $col='', $numrows=-1, $offset=-1, $field="*")
	{
		$where = "mc_auth_id = '{$mc_auth_id}'";
		if($auth_result > '-1') {
			$where .= " and auth_result = '{$auth_result}'";
		}
		$result = $this->getAll($where, $col, $numrows, $offset, $field);
		return (is_array($result)) ? $result : array();
	}

	/**
	 * (功能描述) 查询关联内容
	 *
	 * @param 
	 * @return
	 */
	public function getJoinRecord($mc_auth_id, $col='', $numrows=-1, $offset=-1)
	{
		/*
		SELECT * FROM zy_auth_mc_log zaml 
		LEFT JOIN zy_mc_log zml ON zaml.mc_log_id = zml.mc_log_id
		WHERE zaml.mc_auth_id = 1
		*/
		$sql = "SELECT * FROM zy_auth_mc_log zaml ";
		$sql .= " LEFT JOIN zy_mc_log zml ON zaml.mc_log_id = zml.mc_log_id ";
		$sql .= " WHERE zaml.mc_auth_id = '{$mc_auth_id}'";
		$result = $this->select($sql, $col, $numrows, $offset);
		return (is_array($result)) ? $result : array();
	}

	/**
	 * (功能描述) 获取设备明细相关信息: 根据验收ID
	 *
	 * @param int mc_auth_id 验收ID
	 * @return array
	 */
	public function getDeviceInfo($mc_auth_id = 0, $auth_mc_log_id = 0, $col='', $numrows=-1, $offset=-1)
	{
		$result = array();

		if(intval($mc_auth_id) <= 0) {
			return $result;
		}
		
		$sql = "SELECT zml.mc_cn_name, zml.mc_en_name,zafm.mc_to_project,zafm.put_user_id,zafm.mc_self_no,zpr.plan_reply_no, zsr.contract_no,zml.mc_model, zaml.* ";
		$sql .= " FROM zy_auth_mc_log AS zaml ";
		$sql .= " left join zy_auth_finish_mc zafm on zaml.comp_no=zafm.comp_no";
		$sql .= " LEFT JOIN zy_mc_log AS zml ON zaml.mc_log_id = zml.mc_log_id ";
		$sql .= " LEFT JOIN zy_sell_reply AS zsr ON zsr.sell_reply_id = zml.sell_reply_id ";
		$sql .= " LEFT JOIN zy_plan_reply_mc AS zprm ON zprm.plan_reply_mc_id = zsr.plan_reply_mc_id ";
		$sql .= " LEFT JOIN zy_plan_reply AS zpr ON zpr.plan_reply_id = zprm.plan_reply_id ";
		$sql .= " WHERE zaml.mc_auth_id = '{$mc_auth_id}' ";
		if($auth_mc_log_id > 0) {
			$sql .= " AND zaml.auth_mc_log_id = '{$auth_mc_log_id}'";
		}
		$cn_name = !empty($_GET['cn_name']) ? trim($_GET['cn_name']) : '';
		if($cn_name){
			$sql .= " and zml.mc_cn_name like '%{$cn_name}%'";
		}
		//echo $sql;
		$result = $this->select($sql, $col, $numrows, $offset);
		return (is_array($result)) ? $result : array();
	}

	/**
	 * (功能描述)获取设备明细相关信息: 根据执行记录ID
	 *
	 * @param 
	 * @return
	 */
	public function getMcInfo($transfer_id, $transfer_log_id = 0, $col='', $numrows=-1, $offset=-1)
	{
		$result = array();

		if(intval($transfer_id) <= 0) {
			return $result;
		}

		$sql = "SELECT zml.mc_cn_name, zml.mc_en_name, zml.mc_no, zml.unit, zml.price, zpr.plan_reply_no, zsr.contract_no,zml.mc_model, zmtl.*,zafm.mc_self_no,zpr.plan_reply_id,zprm.plan_reply_mc_id, zsr.sell_reply_id,zml.mc_log_id";
		//$sql = "SELECT zml.mc_cn_name, zml.mc_en_name, zml.mc_no, zml.unit, zml.price,zml.sell_reply_id";
		$sql .= " FROM zy_mc_transfer_log AS zmtl ";
		$sql .= " LEFT join zy_auth_finish_mc as zafm on zmtl.comp_no=zafm.comp_no";
		$sql .= " LEFT JOIN zy_mc_log AS zml ON zml.mc_log_id = zmtl.mc_log_id ";
		$sql .= " LEFT JOIN zy_sell_reply AS zsr ON zsr.sell_reply_id = zml.sell_reply_id  ";
		$sql .= " LEFT JOIN zy_plan_reply_mc AS zprm ON zprm.plan_reply_mc_id = zsr.plan_reply_mc_id  ";
		$sql .= " LEFT JOIN zy_plan_reply AS zpr ON zpr.plan_reply_id = zprm.plan_reply_id  ";
		$sql .= " WHERE zmtl.transfer_id = '{$transfer_id}' ";
		if($transfer_log_id > 0) {
			$sql .= " AND zmtl.transfer_log_id = '{$transfer_log_id}'";
		}
		$cn_name = !empty($_GET['cn_name']) ? trim($_GET['cn_name']) : '';
		if($cn_name){
			$sql .= " and zml.mc_cn_name like '%{$cn_name}%'";
		}
		//echo $sql;
		$result = $this->select($sql, $col, $numrows, $offset);
		return (is_array($result)) ? $result : array();		
	}
    
	
	function del_finish_mc($where)
	{
		//$auth_mc_log_db = Factory::getDB('zy_auth_mc_log');
		$auth_mc_log_arr = $this->getAll($where);
		$this->delete_zy_auth_mc_log($where);
		//$arr = $this->getAll('comp_no='.$auth_mc_log_arr[0]['comp_no']);
		$finsh_db = Factory::getDB('zy_auth_finish_mc');
		//echo "comp_no='{$auth_mc_log_arr[0]['comp_no']}'";
		$arr = $finsh_db->getAll("comp_no='{$auth_mc_log_arr[0]['comp_no']}'");
		//print_r($arr);exit;
		$finsh_db->delete_zy_auth_finish_mc("comp_no='{$auth_mc_log_arr[0]['comp_no']}'");

		$zy_no_log_db = Factory::getDB('zy_no_log');
		$zy_no_arr = $zy_no_log_db->getOne($arr[0]['mc_log_id']);
		//print_r($zy_no_log_db->getDebug());
		//print_r($zy_no_arr);exit;
		//首先数量减少1
		$up_arr = array('auth_no'=>$zy_no_arr['auth_no']-1);
		$zy_no_log_db->update_zy_no_log($up_arr,'mc_log_id='.$arr[0]['mc_log_id']);
		//如果是已经验收完需要将以前的验收状态打开
		if($zy_no_arr['auth_no']==$zy_no_arr['sell_no']){
			//查询验收ID
			$zy_mc_log_db = Factory::getDB('zy_mc_log');
			$zy_mc_log_where = 'mc_log_id='.$arr[0]['mc_log_id'];
			$mc_up_arr = array('auth_state'=>0);
			$mc_log_arr = $zy_mc_log_db->update_zy_mc_log($mc_up_arr,$zy_mc_log_where);			
		}
		
	}
}

/* Public function is the Class method, Design by Hooker framework designer */
/* CreateTime: 2011-07-07 11:56:30 */

?>